{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import tensorflow as tf \n",
    "import matplotlib.pyplot as plt \n",
    "\n",
    "# import input_data.py\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true,
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "from tensorflow.examples.tutorials.mnist import input_data "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Extracting data/train-images-idx3-ubyte.gz\n",
      "Extracting data/train-labels-idx1-ubyte.gz\n",
      "Extracting data/t10k-images-idx3-ubyte.gz\n",
      "Extracting data/t10k-labels-idx1-ubyte.gz\n",
      "done\n"
     ]
    }
   ],
   "source": [
    "mnist = input_data.read_data_sets('data/',one_hot=True)\n",
    "print ('done')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "trainimgs = mnist.train.images\n",
    "trainlabels = mnist.train.labels\n",
    "testimgs = mnist.test.images\n",
    "testlabels = mnist.test.labels\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[36695 35274  4993 39574 46859]\n"
     ]
    }
   ],
   "source": [
    "num_samples = 5\n",
    "randidx = np.random.randint(trainimgs.shape[0],size=num_samples)\n",
    "print(randidx)\n",
    "\n",
    "for i in randidx:\n",
    "    cur_img = np.reshape(trainimgs[i,:],(28,28))\n",
    "    cur_labl = np.argmax(trainlabels[i,:])\n",
    "    plt.matshow(cur_img,cmap = plt.get_cmap('gray'))\n",
    "    plt.title(str(i)+\"th training img,\"+\"label is : \"+str(cur_labl))\n",
    "#     print(str(i)+\"th training img,\"+\"label is : \"+\"str(cur_labl)\")\n",
    "#     plt.show()\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = tf.placeholder('float',[None,784])\n",
    "y = tf.placeholder('float',[None,10])\n",
    "# W = tf.Variable(tf.random_uniform((784,10),minval=0.0,maxval=0.5,dtype=tf.float32))\n",
    "W = tf.Variable(tf.zeros([784,10]))\n",
    "b = tf.Variable(tf.zeros([10]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 逻辑回归model\n",
    "# softmax score\n",
    "score = tf.nn.softmax(tf.matmul(x,W)+b)\n",
    "sum_log_per_row = -tf.reduce_sum(y*tf.log(score),reduction_indices=1)\n",
    "loss = tf.reduce_mean(sum_log_per_row)\n",
    "optm = tf.train.GradientDescentOptimizer(0.01).minimize(loss)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# tf.arg_max( input, dimension, output_type=tf.int64,  name=None )\n",
    "pred = tf.equal(tf.arg_max(score,1),tf.arg_max(y,1))\n",
    "# accuracy calculate 转换为float型再算百分比\n",
    "acc = tf.reduce_mean(tf.cast(pred,'float'))\n",
    "\n",
    "# 初始化\n",
    "init = tf.global_variables_initializer()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "epoch:000/500 loss:0.004150857 train_acc: 0.600 test_acc: 0.397\n",
      "epoch:005/500 loss:0.004063824 train_acc: 0.630 test_acc: 0.618\n",
      "epoch:010/500 loss:0.003983749 train_acc: 0.670 test_acc: 0.673\n",
      "epoch:015/500 loss:0.003910895 train_acc: 0.590 test_acc: 0.589\n",
      "epoch:020/500 loss:0.003770087 train_acc: 0.610 test_acc: 0.593\n",
      "epoch:025/500 loss:0.003751051 train_acc: 0.540 test_acc: 0.654\n",
      "epoch:030/500 loss:0.003684204 train_acc: 0.610 test_acc: 0.679\n",
      "epoch:035/500 loss:0.003559206 train_acc: 0.750 test_acc: 0.661\n",
      "epoch:040/500 loss:0.003512331 train_acc: 0.680 test_acc: 0.690\n",
      "epoch:045/500 loss:0.003438462 train_acc: 0.680 test_acc: 0.715\n",
      "epoch:050/500 loss:0.003258509 train_acc: 0.740 test_acc: 0.741\n",
      "epoch:055/500 loss:0.003338769 train_acc: 0.640 test_acc: 0.745\n",
      "epoch:060/500 loss:0.003345041 train_acc: 0.710 test_acc: 0.757\n",
      "epoch:065/500 loss:0.003192747 train_acc: 0.760 test_acc: 0.761\n",
      "epoch:070/500 loss:0.003127092 train_acc: 0.760 test_acc: 0.768\n",
      "epoch:075/500 loss:0.003080243 train_acc: 0.760 test_acc: 0.761\n",
      "epoch:080/500 loss:0.003034145 train_acc: 0.690 test_acc: 0.768\n",
      "epoch:085/500 loss:0.002855315 train_acc: 0.800 test_acc: 0.767\n",
      "epoch:090/500 loss:0.002898225 train_acc: 0.770 test_acc: 0.781\n",
      "epoch:095/500 loss:0.002915309 train_acc: 0.750 test_acc: 0.785\n",
      "epoch:100/500 loss:0.002775147 train_acc: 0.720 test_acc: 0.780\n",
      "epoch:105/500 loss:0.002748517 train_acc: 0.790 test_acc: 0.790\n",
      "epoch:110/500 loss:0.002787471 train_acc: 0.750 test_acc: 0.791\n",
      "epoch:115/500 loss:0.002750381 train_acc: 0.770 test_acc: 0.787\n",
      "epoch:120/500 loss:0.002772286 train_acc: 0.750 test_acc: 0.802\n",
      "epoch:125/500 loss:0.002632490 train_acc: 0.770 test_acc: 0.809\n",
      "epoch:130/500 loss:0.002561857 train_acc: 0.810 test_acc: 0.808\n",
      "epoch:135/500 loss:0.002366760 train_acc: 0.830 test_acc: 0.804\n",
      "epoch:140/500 loss:0.002444052 train_acc: 0.810 test_acc: 0.806\n",
      "epoch:145/500 loss:0.002497808 train_acc: 0.780 test_acc: 0.807\n",
      "epoch:150/500 loss:0.002439716 train_acc: 0.850 test_acc: 0.806\n",
      "epoch:155/500 loss:0.002410699 train_acc: 0.810 test_acc: 0.805\n",
      "epoch:160/500 loss:0.002393702 train_acc: 0.780 test_acc: 0.805\n",
      "epoch:165/500 loss:0.002318407 train_acc: 0.810 test_acc: 0.808\n",
      "epoch:170/500 loss:0.002466339 train_acc: 0.740 test_acc: 0.814\n",
      "epoch:175/500 loss:0.002159337 train_acc: 0.850 test_acc: 0.820\n",
      "epoch:180/500 loss:0.002190668 train_acc: 0.840 test_acc: 0.818\n",
      "epoch:185/500 loss:0.002201178 train_acc: 0.800 test_acc: 0.818\n",
      "epoch:190/500 loss:0.002290744 train_acc: 0.770 test_acc: 0.822\n",
      "epoch:195/500 loss:0.002242404 train_acc: 0.840 test_acc: 0.827\n",
      "epoch:200/500 loss:0.002230078 train_acc: 0.780 test_acc: 0.826\n",
      "epoch:205/500 loss:0.002135233 train_acc: 0.830 test_acc: 0.826\n",
      "epoch:210/500 loss:0.001981658 train_acc: 0.870 test_acc: 0.826\n",
      "epoch:215/500 loss:0.001955464 train_acc: 0.870 test_acc: 0.827\n",
      "epoch:220/500 loss:0.002073504 train_acc: 0.810 test_acc: 0.828\n",
      "epoch:225/500 loss:0.002094299 train_acc: 0.810 test_acc: 0.829\n",
      "epoch:230/500 loss:0.001933820 train_acc: 0.850 test_acc: 0.831\n",
      "epoch:235/500 loss:0.002162932 train_acc: 0.810 test_acc: 0.833\n",
      "epoch:240/500 loss:0.001851107 train_acc: 0.850 test_acc: 0.832\n",
      "epoch:245/500 loss:0.001836816 train_acc: 0.890 test_acc: 0.832\n",
      "epoch:250/500 loss:0.001990675 train_acc: 0.860 test_acc: 0.834\n",
      "epoch:255/500 loss:0.001953964 train_acc: 0.820 test_acc: 0.834\n",
      "epoch:260/500 loss:0.001961246 train_acc: 0.870 test_acc: 0.835\n",
      "epoch:265/500 loss:0.001755672 train_acc: 0.870 test_acc: 0.833\n",
      "epoch:270/500 loss:0.001803357 train_acc: 0.830 test_acc: 0.832\n",
      "epoch:275/500 loss:0.002177644 train_acc: 0.710 test_acc: 0.833\n",
      "epoch:280/500 loss:0.001856583 train_acc: 0.820 test_acc: 0.834\n",
      "epoch:285/500 loss:0.001763146 train_acc: 0.860 test_acc: 0.834\n",
      "epoch:290/500 loss:0.001868654 train_acc: 0.840 test_acc: 0.833\n",
      "epoch:295/500 loss:0.001686525 train_acc: 0.890 test_acc: 0.834\n",
      "epoch:300/500 loss:0.001757683 train_acc: 0.890 test_acc: 0.835\n",
      "epoch:305/500 loss:0.001902300 train_acc: 0.800 test_acc: 0.835\n",
      "epoch:310/500 loss:0.001765857 train_acc: 0.840 test_acc: 0.837\n",
      "epoch:315/500 loss:0.001703857 train_acc: 0.860 test_acc: 0.838\n",
      "epoch:320/500 loss:0.001643467 train_acc: 0.900 test_acc: 0.837\n",
      "epoch:325/500 loss:0.001825835 train_acc: 0.770 test_acc: 0.837\n",
      "epoch:330/500 loss:0.001728558 train_acc: 0.840 test_acc: 0.837\n",
      "epoch:335/500 loss:0.001860996 train_acc: 0.810 test_acc: 0.837\n",
      "epoch:340/500 loss:0.001912944 train_acc: 0.780 test_acc: 0.839\n",
      "epoch:345/500 loss:0.001587400 train_acc: 0.830 test_acc: 0.838\n",
      "epoch:350/500 loss:0.001568431 train_acc: 0.880 test_acc: 0.836\n",
      "epoch:355/500 loss:0.001768146 train_acc: 0.800 test_acc: 0.837\n",
      "epoch:360/500 loss:0.001617840 train_acc: 0.870 test_acc: 0.838\n",
      "epoch:365/500 loss:0.001942390 train_acc: 0.780 test_acc: 0.838\n",
      "epoch:370/500 loss:0.001510163 train_acc: 0.900 test_acc: 0.837\n",
      "epoch:375/500 loss:0.001698334 train_acc: 0.880 test_acc: 0.839\n",
      "epoch:380/500 loss:0.001852896 train_acc: 0.790 test_acc: 0.840\n",
      "epoch:385/500 loss:0.001577577 train_acc: 0.860 test_acc: 0.839\n",
      "epoch:390/500 loss:0.001504150 train_acc: 0.880 test_acc: 0.839\n",
      "epoch:395/500 loss:0.001730346 train_acc: 0.790 test_acc: 0.840\n",
      "epoch:400/500 loss:0.001681040 train_acc: 0.820 test_acc: 0.841\n",
      "epoch:405/500 loss:0.001492322 train_acc: 0.870 test_acc: 0.843\n",
      "epoch:410/500 loss:0.001772476 train_acc: 0.790 test_acc: 0.843\n",
      "epoch:415/500 loss:0.001560566 train_acc: 0.840 test_acc: 0.843\n",
      "epoch:420/500 loss:0.001625727 train_acc: 0.820 test_acc: 0.842\n",
      "epoch:425/500 loss:0.001507806 train_acc: 0.850 test_acc: 0.843\n",
      "epoch:430/500 loss:0.001463987 train_acc: 0.880 test_acc: 0.845\n",
      "epoch:435/500 loss:0.001559419 train_acc: 0.890 test_acc: 0.844\n",
      "epoch:440/500 loss:0.001581169 train_acc: 0.800 test_acc: 0.845\n",
      "epoch:445/500 loss:0.001550002 train_acc: 0.820 test_acc: 0.846\n",
      "epoch:450/500 loss:0.001636143 train_acc: 0.780 test_acc: 0.846\n",
      "epoch:455/500 loss:0.001363450 train_acc: 0.870 test_acc: 0.845\n",
      "epoch:460/500 loss:0.001415040 train_acc: 0.880 test_acc: 0.845\n",
      "epoch:465/500 loss:0.001348041 train_acc: 0.890 test_acc: 0.845\n",
      "epoch:470/500 loss:0.001468201 train_acc: 0.830 test_acc: 0.845\n",
      "epoch:475/500 loss:0.001486184 train_acc: 0.850 test_acc: 0.847\n",
      "epoch:480/500 loss:0.001568967 train_acc: 0.810 test_acc: 0.848\n",
      "epoch:485/500 loss:0.001543121 train_acc: 0.830 test_acc: 0.849\n",
      "epoch:490/500 loss:0.001472670 train_acc: 0.820 test_acc: 0.850\n",
      "epoch:495/500 loss:0.001373929 train_acc: 0.860 test_acc: 0.848\n",
      "finish\n"
     ]
    }
   ],
   "source": [
    "epochs = 500\n",
    "batch_size = 100\n",
    "display_step = 5\n",
    "\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    sess.run(init)\n",
    "    \n",
    "    for epoch in range(epochs):\n",
    "        avg_loss = 0\n",
    "        num_batch = int(mnist.train.num_examples/batch_size)\n",
    "        batch_xs,batch_ys = mnist.train.next_batch(batch_size)\n",
    "        feeds = {x:batch_xs,y:batch_ys}\n",
    "        \n",
    "        sess.run(optm,feed_dict=feeds)\n",
    "        avg_loss += sess.run(loss,feed_dict=feeds)/num_batch\n",
    "        \n",
    "        # display\n",
    "        if epoch%display_step == 0:\n",
    "            feeds_train = {x:batch_xs,y:batch_ys}\n",
    "            feed_test = {x:mnist.test.images,y:mnist.test.labels}\n",
    "            train_acc = sess.run(acc,feed_dict=feeds_train)\n",
    "            test_acc = sess.run(acc,feed_test)\n",
    "            print ('epoch:%03d/%03d loss:%.9f train_acc: %.3f test_acc: %.3f'\n",
    "                  %(epoch,epochs,avg_loss,train_acc,test_acc))\n",
    "    print('finish')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
